package com.example.administrator.mobile;


import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class Recycle extends Fragment {
    private static final String TAG = "Recycle";

   AppAdapter mAppAdapter;
    Handler mHandler;
    ListView mlvAz;


    public Recycle() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
       View view = inflater.inflate(R.layout.fragment_recycle, container, false);
       mlvAz = view.findViewById(R.id.lv_az);
       return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mHandler = new Handler();
        mlvAz =getView().findViewById(R.id.lv_az);
        mAppAdapter = new AppAdapter();
        mlvAz.setAdapter(mAppAdapter);
        initData();

    }


    private void initData() {
        new Thread(){
            @Override
            public void run() {
                super.run();
                //扫描得到APP列表
                final List<MyAppInfo> appInfos = ApkTool.scanLocalInstallAppList(Recycle.this.getActivity().getPackageManager());
                Log.e(TAG, "run: "+appInfos);
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mAppAdapter.setData(appInfos);
                    }
                });


            }
        }.start();

    }

    class AppAdapter extends BaseAdapter {

        List<MyAppInfo> myAppInfos;

        public void setData(List<MyAppInfo> myAppInfos) {
            this.myAppInfos = myAppInfos;
            notifyDataSetChanged();
        }

        public List<MyAppInfo> getData() {
            return myAppInfos;
        }

        @Override
        public int getCount() {
            if (myAppInfos != null && myAppInfos.size() > 0) {
                return myAppInfos.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            if (myAppInfos != null && myAppInfos.size() > 0) {
                return myAppInfos.get(position);
            }
            return myAppInfos;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
           AppAdapter.ViewHolder mViewHolder;
            MyAppInfo myAppInfo = myAppInfos.get(position);
            if (convertView == null) {
                mViewHolder = new AppAdapter.ViewHolder();
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.list_item_layout, null);
                mViewHolder.iv_icon = (ImageView) convertView.findViewById(R.id.iv_icon);
                mViewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
                mViewHolder.tv_id=convertView.findViewById(R.id.tv_id);
                convertView.setTag(mViewHolder);
            } else {
                mViewHolder = (AppAdapter.ViewHolder) convertView.getTag();
            }
            mViewHolder.iv_icon.setImageDrawable(myAppInfo.getImage());
            mViewHolder.tv_name.setText(myAppInfo.getAppName());
            mViewHolder.tv_id.setText(myAppInfo.getCode());
            return convertView;
        }



        class ViewHolder {

            ImageView iv_icon;
            TextView tv_name;
            TextView tv_id;
        }
    }

    public static class MyAppInfo {
        private Drawable image;
        private String appName;
        private String  code;

        public MyAppInfo(Drawable image, String appName,String code) {
            this.image = image;
            this.appName = appName;
            this.code =code;
        }
        public MyAppInfo() {

        }

        public Drawable getImage() {
            return image;
        }

        public void setImage(Drawable image) {
            this.image = image;
        }

        public String getAppName() {
            return appName;
        }

        public void setAppName(String appName) {
            this.appName = appName;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String  getCode() {
            return code;
        }

        @Override
        public String toString() {
            return "MyAppInfo{" +
                    "image=" + image +
                    ", appName='" + appName + '\'' +
                    ", code='" + code + '\'' +
                    '}';
        }
    }
    public static class ApkTool {
        static String TAG = "ApkTool";
        List<MyAppInfo> mLocalInstallApps = null;

        static List<MyAppInfo> scanLocalInstallAppList(PackageManager packageManager) {
            List<MyAppInfo> myAppInfos = new ArrayList<MyAppInfo>();
            try {
                List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
                for (int i = 0; i < packageInfos.size(); i++) {
                    PackageInfo packageInfo = packageInfos.get(i);
                    //过滤掉系统app
                    //                   if ((ApplicationInfo.FLAG_SYSTEM & packageInfo.applicationInfo.flags) != 0) {
                    //                       continue;
                    //                 }
                    MyAppInfo myAppInfo = new MyAppInfo();
                    myAppInfo.setAppName((String) packageInfo.applicationInfo.loadLabel(packageManager));
                    myAppInfo.setCode(packageInfo.versionName);
                    if (packageInfo.applicationInfo.loadIcon(packageManager) == null) {
                        continue;
                    }
                    myAppInfo.setImage(packageInfo.applicationInfo.loadIcon(packageManager));
                    myAppInfos.add(myAppInfo);
                }
            }catch (Exception e){
                Log.e(TAG,"===============获取应用包信息失败");
            }
            return myAppInfos;
        }

    }

}

